package com.tuo51.evaluate.domain.childrens;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.Domain;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.utils.DateUtil;
import com.tuo51.foudation.utils.ModelUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * *******************************
 * Created by Ron.
 * form com.tuo51
 * on 2017/7/13
 * *******************************
 */
@Entity
@Data
@Table(name = "children")
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class Children extends Domain {

    @Column(columnDefinition = " varchar(50)  COMMENT '宝宝姓名'")
    private String name;

    @Column(columnDefinition = " varchar(32)  COMMENT '机构id'")  //默认的
    private String orgId;

    @Column(columnDefinition = " varchar(50)  COMMENT '宝宝昵称'")
    private String nickName="";

    @Column(columnDefinition = " int default 0  COMMENT '宝宝性别 ：0：女，1：男'")
    private Integer gender;

//    @Column(columnDefinition = " varchar(255)  COMMENT '地址'")
//    private String address = "";

    @Column(columnDefinition = " datetime COMMENT '宝宝生日'")
    private Date birthday;

    @Column(columnDefinition = " varchar(50)  COMMENT '体重'")
    private String weight;

    @Column(columnDefinition = " varchar(50)  COMMENT '身高'")
    private String height;

    @Column(columnDefinition = " varchar(50)  COMMENT '头围'")
    private String header;

    @Column(columnDefinition = " varchar(255)  COMMENT '备注'")
    private String remark = "";

    @Column(columnDefinition = " varchar(512)  COMMENT '头像地址'")
    private String imgUrl;

    @Column(columnDefinition = " varchar(32)  COMMENT '录入员工id'")
    private String staffId;

    @Column(columnDefinition = " datetime COMMENT '最近一次测评日期'")
    private Date lastEvaluateDate;

    @Column(columnDefinition = " datetime COMMENT '下次测评日期'")
    private Date nextEvaluateDate;

    @Transient
    private Integer month;
    public Integer getMonth(){
        if (month == null && birthday != null){
            setMonth(DateUtil.betweenMonths(new Date(), getBirthday()));
        }
        return month;
    }

    //    @Transient
    @JsonIgnore
    @OneToOne(mappedBy = "children")
    private ChildrenExtendInfo extendInfo;

    public Children(String childrenName, String nickName, Integer gender, Date birthday, String weight, String height, String header, String address){
        this.setName(childrenName);
        this.setNickName(nickName);
        this.setGender(gender);
        this.setBirthday(birthday);
        this.setHeight(height);
        this.setWeight(weight);
        this.setHeader(header);
    }

    public static Children of(String name, String nickName, Integer gender, Date birthday, String address, String staffId){
        Children children = new Children();
        children.setName(name);
        children.setNickName(nickName);
        children.setBirthday(birthday);
//        children.setAddress(address);
        children.setStaffId(staffId);
        children.setGender(gender);
        return children;
    }

    public static Children of_new(String name, String nickName, Integer gender, Date birthday, String address, String staffId,String weight,String height,String header,String remark){
        Children children = new Children();
        children.setName(name);
        children.setNickName(nickName);
        children.setBirthday(birthday);
//        children.setAddress(address);
        children.setStaffId(staffId);
        children.setGender(gender);
        children.setWeight(weight);
        children.setHeader(header);
        children.setHeight(height);
        children.setRemark(remark);
        return children;
    }


    public Children merge(Children children) {
        try {
            ModelUtil.copyNotNullPropertiesExcludeId(this, children);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.ENTITY_MERGE_ERROR, "拷贝属性出错");
        }
        return this;
    }

}
